package pub.tbc.hh.mall.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * @author tbc  by 2018/5/26
 */
@Mapper
public interface ShoppingCartMapper {

    @Insert("insert into t_shopping_cart(user_id, wares_id, number)values(#{userId}, #{waresId}, #{number}")
    int add(Map<String, Object> param);

    @Insert("<script>" +
            "insert into t_shopping_cart(user_id, wares_id, number)values" +
            "<foreach item=\"item\" index=\"index\" collection=\"list\"\n open=\"(\" separator=\",\" close=\")\">\n" +
            "    #{item}\n" +
            "</foreach>" +
            "</script>")
    int[] addBatch(Map<String, Object> params);

    @Select("select wares_id waresId, number from t_shopping_cart " +
            "where wares_id = #{waresId} and is_remove=0 and user_id = #{userId}")
    List<Map<String, Object>> queryWaresByWaresId(long waresId, int userId);

    @Select("select wares_id waresId, number from t_shopping_cart " +
            "where wares_id = #{waresId} and is_remove=0 and user_id = #{userId} for update")
    List<Map<String, Object>> queryWaresByWaresIdAndLock(long waresId);

    @Update("update t_shopping_cart set number = #{number} where user_id = #{userId} and wares_id = #{waresId}  ")
    int updateNum(Map<String, Object> param);

    @Select("select * from t_shopping_cart where is_remove = 0 and user_id = #{userId}")
    List<Map<String, Object>> queryWaresByUserId(int userId);


}
